using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Game1
{
    class ChooseServerScreen:Screen
    {
        GuiManager guiManager;
        Client client = new Client();
        SubMenu subMenu;
        int listSize;
        
        protected override void LoadContent()
        {
            client.findLocalServers();
            guiManager = GuiManager.Singleton;
            guiManager.removeAllEntries();
            guiManager.AddMenuEntry("ChooseServer", "Escolha o servidor:", 50, 50, 1.5f, Color.Gray,35,25);
            subMenu = guiManager.createSubMenu("ChooseServerMenu", 100, 100, Color.Gray, new Color(0, 50, 130));

            //server selection options
            subMenu.AddMenuEntry("UpdateList", "Update servers list", 0, 400,30,25);
            //apenas para teste
            subMenu.AddMenuEntry("Cancel", "Cancel", 200, 400, 55, 25);

            guiManager.setActiveSubMenu("ChooseServerMenu");          
        }

        
        public override void Update(GameTime gameTime)
        {
            
            if (Input.IsKeyPressed(Keys.Enter))
            {
                string selected = guiManager.getActiveSubMenu().getSelected().getName();

                switch (selected)
                {                  
                    case "UpdateList":
                        int i = 0;
                        guiManager.setActiveSubMenu("ChooseServer");
                        //funcao para busca de servidores
                        client.findLocalServers();
                        //criar menus com nomes dos servidores encontrados
                        for (; (client.serverNameIP[i][0]!=null)&&(i<Config.Singleton.ServerListLimit); i++)
                        {
                            subMenu.AddMenuEntry(i.ToString(), client.serverNameIP[i][0], 0, 30*i,30,25);            
                        }
                        listSize=i;
                        break;

                        //apenas para teste
                    case "Cancel":
                        //volta ao menu
                        Base.currentScreen = new MenuScreen();
                        break;
                        
                }

                for (int j = 0; j <= listSize; j++)
                {
                    if (selected == j.ToString())
                    {
                        client.clientConnect(client.serverNameIP[j][1]);
                        Base.currentScreen = new InGameScreen(client);
                    }
                }
            }
            if (Input.IsKeyPressed(Keys.Escape))
            {
                Base.currentScreen = new MenuScreen();
            }

            if (Input.IsKeyPressed(Keys.Down) || Input.IsKeyPressed(Keys.Left))
            {
                guiManager.selectUp();
            }

            if (Input.IsKeyPressed(Keys.Up) || Input.IsKeyPressed(Keys.Right))
            {
                guiManager.selectDown();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.White);
        }
    }
}
